package com.tyqx.hashchainbiz.controller.user.auth;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.TrackUser;
import com.tyqx.hashchainbean.vo.TrackUserVO;
import com.tyqx.hashchainbiz.service.TokenService;
import com.tyqx.hashchainbiz.service.TrackUserService;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import hashchainutil.exception.HashchainException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: dcep-spring
 * @description:
 * @author: Mr.Li
 * @create: 2020-04-19 00:28
 **/
@Api(description = "用户登录")
@RestController
@RequestMapping("/trackUserLogin")
@CrossOrigin
public class TrackUserLandController {

    @Resource
    private TrackUserService trackUserService;

    @Resource
    private TokenService tokenService;


    Logger logger=LoggerFactory.getLogger(this.getClass());

    //    Logger logger= LoggerFactory.getLogger(this.getClass());
    @ApiOperation(value = "用户登录", httpMethod = "POST",
            produces = "application/json",
            response = Dto.class,
            notes = "用户登录,服务器获取账号和密码 \n" +
                    "\n返回VO类DcepUserVO \n" +
                    "\n返回结果:      \n" +
                    "{\n" +
                    "\n  \"success\": \"true\",\n" +
                    "\n  \"errorCode\": \"0\",\n" +
                    "\n  \"msg\": \"\",\n" +
                    "\n  \"data\": {\n" +
                    "\n  }\n" +
                    "\n}\n" +
                    "\n错误码:\n" +
                    "\n100005:未注册\n")
    @RequestMapping(value="/loginTrackUser", method = RequestMethod.POST,
            produces = "application/json")
    @ResponseBody
    public Dto loginTrackUser(@RequestBody TrackUser trackUser, HttpServletRequest request) {
        try {
            TrackUserVO trackUserVO = trackUserService.login(trackUser.getUserName(), trackUser.getPassword());
            return DtoUtil.returnDataSuccess(trackUserVO,"登录成功");
        } catch (HashchainException e) {
            if (e.getErrorCode().equals("100005")) {
                return DtoUtil.returnSuccess(e.getErrorCode(), e.getMessage());
            }
            return DtoUtil.returnFail(e.getMessage(), e.getErrorCode());
        }


    }

    @ApiOperation(value = "退出",response = Dto.class,httpMethod = "GET",produces = "application/json",
    notes = "验证token是否有效\n" +
            "\n退出成功: \n" +
            "{\n" +
            "\n  \"success\": \"true\",\n" +
            "\n  \"errorCode\": \"0\",\n" +
            "\n  \"msg\": \"\",\n" +
            "\n  \"data\": {}\n" +
            "\n}\n" +
            "\n退出失败: \n" +
            "{\n" +
            "\n  \"success\": \"false\",\n" +
            "\n  \"errorCode\": \"100001\",\n" +
            "\n  \"msg\": \"token无效\",\n" +
            "\n  \"data\": {}\n" +
            "\n}")
    @RequestMapping(value = "/logout", method = RequestMethod.GET,
            produces = "application/json")
    //指定参数说明
    @ApiImplicitParam(required = true,value = "token",name="token",paramType = "header")
    @ResponseBody
    public  Dto logout(HttpServletRequest request){
        String token=request.getHeader("token");
        boolean validate=tokenService.validateToken(token);
        if(validate){
            tokenService.removeToken(token);
            return DtoUtil.returnSuccess();
        }else {
            return DtoUtil.returnFail("token无效","100001");
        }
    }
}
